#include"sort.h"
#include"Stack.h"
void Swap(int* a, int* b) {
	int tmp = *a;
	*a = *b;
	*b = tmp;
}

void Print(int* arr, int sz) {
	for (size_t i = 0; i < sz; i++)
	{
		printf("%d ", arr[i]);
	}
	printf("\n");
}

void BubbleSort(int* arr, int size) {
	for (int i = 0; i < size - 1; i++) {
		int flag = 0;
		for (int j = 0; j < size - 1 - i; j++) {
			if (arr[j] > arr[j + 1]) {
				Swap(&arr[j], &arr[j + 1]);
				flag = -1;
			}
		}
		if (flag == 0) break;
	}
}
void InsertSort(int* arr, int size) {
	for (int i = 0; i < size - 1; i++) {
		int end = i;
		int tmp = arr[i + 1];
		while (end >= 0) {
			if (tmp < arr[end]) {
				arr[end + 1] = arr[end];
				end--;
			}
			else break;
		}
		arr[end + 1] = tmp;
	}
}
void ShellSort(int* arr, int size) {
	int gap = size;
	while (gap > 1) {
		gap = gap / 3 + 1;
		for (int i = 0; i < size - gap; i++) {
			int end = i;
			int tmp = arr[end + gap];
			while (end >= 0) {
				if (arr[end] > tmp) {
					arr[end + gap] = arr[end];
					end -= gap;
				}
				else break;
			}
			arr[end + gap] = tmp;
		}
	}
}
void SelectSort(int* arr, int sz) {
	int begin = 0;
	int end = sz - 1;
	while (begin < end) {
		int mini = begin;
		int maxi = begin;
		for (int i = begin + 1; i <= end; i++) {
			if (arr[i] < arr[mini]) {
				mini = i;
			}
			if (arr[i] > arr[maxi]) {
				maxi = i;
			}
		}
		if (maxi == begin) {
			maxi = mini;
		}
		Swap(&arr[mini], &arr[begin]);
		Swap(&arr[maxi], &arr[end]);
		begin++;
		end--;
	}
}
//int _QuickSort(int* arr, int left, int right) {
//	int keyi = left;
//	++left;
//	while (left <= right) {
//		while (left <= right && arr[left] < arr[keyi]) left++;
//		while (left <= right && arr[right] > arr[keyi]) right--;
//		if (left <= right) Swap(&arr[left], &arr[right]);
//	}
//	Swap(&arr[right], &arr[keyi]);
//	return right;
//}//霍尔法


//int _QuickSort(int* arr, int left, int right) {
//	int key = arr[left];
//	int hole = left;
//	while (left < right) {
//		while (left < right && arr[right] > key)
//			right--;
//		arr[hole] = arr[right];
//		hole = right;
//		while (left < right && arr[left] < key)
//			left++;
//		arr[hole] = arr[left];
//		hole = left;
//	}
//	arr[hole] = key;
//	return hole;
//}//挖坑法

int _QuickSort(int* arr, int left, int right) {
	int cur = left + 1;
	int prev = left;
	int keyi = left;
	while (cur <= right) {
		if(arr[cur] < arr[prev] && ++prev != cur) {
			Swap(&arr[cur], &arr[prev]);
		}
		cur++;
	}
	Swap(&arr[keyi], &arr[prev]);
	return keyi;
}

void QuickSort(int* arr, int left, int right) {
	if (left >= right) return;
	int keyi = _QuickSort(arr, left, right);
	QuickSort(arr, left, keyi - 1);
	QuickSort(arr, keyi + 1, right);
}

void QuickSortNonR(int* arr, int left, int right) {
	Stack st;
	StackInit(&st);
	StackPush(&st, left);
	StackPush(&st, right);
	while (!StackEmpty(&st)) {
		int end = StackTop(&st);
		StackPop(&st);
		int begin = StackTop(&st);
		StackPop(&st);

		int cur = begin + 1;
		int prev = begin;
		int keyi = begin;
		while (cur <= end) {
			if (arr[cur] < arr[keyi] && ++prev != cur) {
				Swap(&arr[cur], &arr[prev]);
			}
			cur++;
		}
		Swap(&arr[keyi], &arr[prev]);
		keyi = prev;

		if (keyi - 1 > begin) {
			StackPush(&st, begin);
			StackPush(&st, keyi - 1);
		}
		if (keyi + 1 < end) {
			StackPush(&st, keyi + 1);
			StackPush(&st, end);
		}
	}
	StackDestory(&st);
}